﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using HiStaff.Util;
using HiStaff.Domain.CO;

namespace HiStaff.iTime
{
    public static class TimeUtil
    {
        private const string ITIME_CFN_FOLDER = "Xml";//"ITimeCfn";
        private const string ITIME_CFN_MNG = "ConfigurationManager.xml";

        private const string XMLSIGNDISPLAY_TAG = "XMLSignDisplay";
        private const string SIGNCOLUMN_TAG = "SignColumn";
        private const string SIGN_ID_ATR = "Id";
        private const string SIGN_WINDEX_ATR = "Windex";
        private const string SIGN_WDATATYPE_ATR = "WDataType";
        private const string SIGN_FORMATSTR_ATR = "FormatString";
        private const string TIMECFN_TEMPLATEFILE = "Default.xml";

        private const string XMLCONFIGMANAGER_TAG = "ConfigManager";
        private const string CONFIGMANAGER_ID_ATR = "Id";
        private const string CONFIGMANAGER_VALUE_ATR = "Value";

        public static List<TIME_CFN> GetSupportDisplayColumn(string formname)
        {
            decimal outval;
            List<TIME_CFN> rtnLstVal = new List<TIME_CFN>();
            TIME_CFN objLang;
            XmlDocument xmlDoc = new XmlDocument();
            string urlSource = AppDomain.CurrentDomain.BaseDirectory + ITIME_CFN_FOLDER + "\\" + formname + ".xml";
            if (!File.Exists(urlSource))
            {
                return rtnLstVal;
            }

            xmlDoc.Load(urlSource);

            // Read Report Note by Id
            XmlNode rptNode = xmlDoc.SelectSingleNode(XMLSIGNDISPLAY_TAG);
            foreach (var item in rptNode.ChildNodes)
            {
                objLang = new TIME_CFN();
                objLang.Id = ((XmlElement)item).Attributes[SIGN_ID_ATR].Value;
                objLang.Text = ((XmlElement)item).InnerText;
                if (((XmlElement)item).Attributes[SIGN_WINDEX_ATR] != null &&
                    decimal.TryParse(((XmlElement)item).Attributes[SIGN_WINDEX_ATR].Value, out outval))
                {
                    objLang.Windex = outval;
                }
                if (((XmlElement)item).Attributes[SIGN_WDATATYPE_ATR] != null &&
                    decimal.TryParse(((XmlElement)item).Attributes[SIGN_WDATATYPE_ATR].Value, out outval))
                {
                    objLang.WDataType = outval;
                }

                if (((XmlElement)item).Attributes[SIGN_FORMATSTR_ATR] != null &&
                    decimal.TryParse(((XmlElement)item).Attributes[SIGN_FORMATSTR_ATR].Value, out outval))
                {
                    objLang.FormatString = ((XmlElement)item).Attributes[SIGN_FORMATSTR_ATR].Value;
                }

                rtnLstVal.Add(objLang);
            }

            return rtnLstVal;
        }

        public static bool UpdateSupportDisplayColumn(List<TIME_CFN> lstSource, string formname)
        {
            string source = string.Empty;
            XmlDocument xmlDoc = new XmlDocument();
            XmlAttribute attItem;
            XmlText xmlVal;
            XmlElement elmRoot;
            XmlElement elmNew;

            string urlSource = AppDomain.CurrentDomain.BaseDirectory + ITIME_CFN_FOLDER;

            if (!Directory.Exists(urlSource))
            {
                Directory.CreateDirectory(urlSource);
            }

            urlSource += "\\" + formname + ".xml";

            source = AppDomain.CurrentDomain.BaseDirectory + ITIME_CFN_FOLDER + "\\" + TIMECFN_TEMPLATEFILE;
            xmlDoc.Load(source);

            // Add new node to xml
            foreach (var sItem in lstSource)
            {
                elmRoot = xmlDoc.DocumentElement;
                elmNew = xmlDoc.CreateElement(SIGNCOLUMN_TAG);
                xmlVal = xmlDoc.CreateTextNode(sItem.Text);
                elmNew.AppendChild(xmlVal);
                attItem = xmlDoc.CreateAttribute(SIGN_ID_ATR);
                attItem.Value = sItem.Id;
                elmNew.Attributes.Append(attItem);
                attItem = xmlDoc.CreateAttribute(SIGN_WINDEX_ATR);
                attItem.Value = sItem.Windex.ToString();
                elmNew.Attributes.Append(attItem);
                attItem = xmlDoc.CreateAttribute(SIGN_WDATATYPE_ATR);
                attItem.Value = sItem.WDataType.ToString();
                elmNew.Attributes.Append(attItem);
                attItem = xmlDoc.CreateAttribute(SIGN_FORMATSTR_ATR);
                attItem.Value = sItem.FormatString;
                elmNew.Attributes.Append(attItem);
                elmRoot.AppendChild(elmNew);
            }

            xmlDoc.Save(urlSource);

            return true;
        }

        //public static List<CO_CONFIG_MNG> ReadAllConfigItems()
        //{
        //    List<CO_CONFIG_MNG> rtnLstVal = new List<CO_CONFIG_MNG>();
        //    CO_CONFIG_MNG objLang;
        //    XmlDocument xmlDoc = new XmlDocument();
        //    string urlSource = AppDomain.CurrentDomain.BaseDirectory + ITIME_CFN_FOLDER + "\\" + ITIME_CFN_MNG;
        //    if (!File.Exists(urlSource))
        //    {
        //        return rtnLstVal;
        //    }

        //    xmlDoc.Load(urlSource);

        //    // Read ROOT Note by Id
        //    XmlNode rptNode = xmlDoc.SelectSingleNode(XMLCONFIGMANAGER_TAG);
        //    foreach (var item in rptNode.ChildNodes)
        //    {
        //        objLang = new CO_CONFIG_MNG();
        //        objLang.ID = ((XmlElement)item).Attributes[CONFIGMANAGER_ID_ATR].Value;
        //        objLang.Value = ((XmlElement)item).Attributes[CONFIGMANAGER_VALUE_ATR].Value;
        //        rtnLstVal.Add(objLang);
        //    }

        //    return rtnLstVal;
        //}

        //public static string ReadConfigItembyKey(string key)
        //{
        //    CO_CONFIG_MNG objResult = StaticObject.ConfigManager.Find(obj => obj.ID == key);
        //    return objResult == null ? string.Empty : objResult.Value;
        //}
        //#region Doc du lieu
        //public static DataTable importRosterExcel(string Filename, int iColEnd)
        //{
        //    return importRosterExcel(Filename, iColEnd, 1);
        //}
        //public static DataTable importRosterExcel(string Filename, int iColEnd, int iRowNumber)
        //{
        //    string mstr_FileName = Filename;
        //    string mstr_PathFileName = mstr_FileName;

        //    Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook();
        //    wb.Open(mstr_FileName);
        //    Aspose.Cells.Worksheet ws = wb.Worksheets[0];

        //    DataTable dt = new DataTable();
        //    int iRowEnd = GetEndRow(ws, iRowNumber + 1);
        //    try
        //    {
        //        for (int j = 0; j < iColEnd; j++)
        //        {
        //            if (ws.Cells[iRowNumber - 1, j].Value == null)
        //                dt.Columns.Add(new DataColumn());
        //            else
        //            {
        //                object value = ws.Cells[iRowNumber - 1, j].Value;
        //                if (value.GetType() == typeof(DateTime))
        //                    dt.Columns.Add(new DataColumn(string.Format("{0:dd/MM/yyyy}", value)));
        //                else dt.Columns.Add(new DataColumn(value.ToString().Replace("\r\n", "").Replace("\n", "").Trim()));
        //            }
        //        }
        //        int i = iRowNumber;
        //        for (; i < iRowEnd; i++)
        //        {
        //            DataRow dr = dt.NewRow();
        //            for (int j = 0; j < iColEnd; j++)
        //            {
        //                object value = ws.Cells[i, j].Value;
        //                if (value == null)
        //                {
        //                    dr[j] = "";
        //                }
        //                else if (value.GetType() == typeof(DateTime))
        //                {
        //                    dr[j] = string.Format("{0:HH:mm}", value);
        //                }
        //                else dr[j] = value.ToString().Trim();
        //            }
        //            dt.Rows.Add(dr);
        //        }
        //        return dt;
        //    }
        //    catch (Exception ex)
        //    {
        //        HiStaff.Util.Log.Instance.WriteExceptionLog(ex, "importExcel");
        //    }
        //    return dt;
        //}
        //private static int GetEndCol(Aspose.Cells.Worksheet ws)
        //{
        //    int iResult = 0;
        //    while (true)
        //    {
        //        string sValue = ws.Cells[0, iResult].Value == null ? "" : ws.Cells[1, iResult].Value.ToString();
        //        if (sValue != "")
        //        {
        //            iResult++;
        //        }
        //        else break;
        //    }
        //    return iResult;
        //}

        //private static int GetEndRow(Aspose.Cells.Worksheet ws, int StartRow)
        //{
        //    while (true)
        //    {
        //        string sValue = ws.Cells[StartRow, 0].Value == null ? "" : ws.Cells[StartRow, 0].Value.ToString();
        //        if (sValue != "")
        //        {
        //            StartRow++;
        //        }
        //        else break;
        //    }
        //    return StartRow;
        //}
        //#endregion
    }
}
